#include "Scene.h"

Scene::Scene (Camera *cam)
{
	this->cam = cam;
}

Scene::Scene (Scene scene,char *ruta):
n_objects(0) 
{
  //scene.importar(ruta);
}

Scene::~Scene ()
{
}

GLuint
Scene::add_object (Object3D *o)
{
	objects.push_back (o);
	return objects.size ();
}

void
Scene::delete_object (GLuint n)
{
	objects.erase (objects.begin () + n);
}

GLuint
Scene::select_all ()
{
	for (vector<Object3D *>::iterator it = objects.begin (); it != objects.end (); it ++)
		(*it)->set_state (SELECTED);
	return objects.size ();
}

void
Scene::deselect_all ()
{
	for (vector<Object3D *>::iterator it = objects.begin (); it != objects.end (); it ++)
 		(*it)->set_state (NORMAL);
}

GLuint
Scene::select_inverse ()
{
	int count = 0;
	for (vector<Object3D *>::iterator it = objects.begin (); it != objects.end (); it ++)
		if ((*it)->get_state () != NORMAL)
			(*it)->set_state (NORMAL);
		else
			{
				(*it)->set_state (SELECTED);    
				count++;
			}
	return count;
}

GLuint
Scene::select_object (GLuint n)
{
	if (n + 1 > objects.size ())
		return 1;       
	objects.at (n)->set_state (SELECTED);
	return 0;
}

void
Scene::deselect_object (GLuint n)
{
	if (n + 1 > objects.size ())
		return;
	objects.at (n)->set_state (NORMAL);
}

GLuint 
Scene::edit_object (GLuint n)
{
	if (n + 1 > objects.size ())
		return 1;
	this->dont_edit ();
	objects.at(n)->set_state (EDITED);
	return 0;
}

void 
Scene::dont_edit ()
{
	for (vector<Object3D *>::iterator it = objects.begin (); it != objects.end (); it ++)
		(*it)->set_state (NORMAL);
}

void 
Scene::move (Vertex3D pos)
{
	for (vector<Object3D *>::iterator it = objects.begin (); it != objects.end (); it ++)
		if ((*it)->get_state () == SELECTED)
			(*it)->move (pos);
}

void 
Scene::scale (GLdouble factor, TAxis axis)
{
	for (vector<Object3D *>::iterator it = objects.begin (); it != objects.end (); it ++)
		if ((*it)->get_state () == SELECTED)
			(*it)->scale (factor, axis);
}

void 
Scene::rotate (GLdouble degree, TAxis axis)
{
	for (vector<Object3D *>::iterator it = objects.begin (); it != objects.end (); it ++)
		if ((*it)->get_state () == SELECTED)
			(*it)->rotate (degree, axis);
}

void
Scene::draw ()
{
  int n=0;
  for (vector<Object3D *>::iterator it = objects.begin (); it != objects.end (); it ++){
    glLoadName(n);
    n++;
    (*it)->draw ();
  }
}

Camera *
Scene::get_cam()
{
	return cam;
}

vector<Object3D*> 
Scene::get_objects()
{
  return objects;
}
